<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
	Name	: accounting class controller
	Author	: Iswan Putera S.Kom
	Date	: 06/09/2013
	Version : 1.0.0
*/

class Accounting extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->library('zetro_auth');
		$this->load->model('main_model');
		$this->zetro_auth->check_login();
		//force_ssl();
	}
	/**
		call view for list transaksi
		
	*/
	function return_trans()
	{
		$fld="<span class='add-on'>Find</span><input type='search' class='span3' id='cari' name='cari' value='' placeholder='Find by name'/>";
		$this->zetro_form->AddUb($fld);
		$this->zetro_form->AddFilter($this->zetro_form->FilterTgl(true,false,'find'));
		$data['tab_header']=$this->zetro_form->generate_tab_header('Koreksi Transaksi',true);
		//generate form and new lokasi
		$this->zetro_form->frm_file('asset/frm/zetro_simpanan.frm');
		$this->zetro_auth->menus('list_transaksi');
			$this->zetro_form->buttons(false,"Simpan");
			$this->zetro_form->section('frmreturn');
			$data['frm2']=$this->zetro_form->frmInline('frm2',false,true);
			//create list
			$content='';
			$this->zetro_form->section('listreturn');
			$this->zetro_form->action_col(true);
			$this->zetro_form->tabel_content($content);
			$data['list']=$this->zetro_form->list_std();
		//generate popup windows edit transaksi
		$data['simp']=$this->zetro_form->generate_dropdown_content('simpanan_jenis','id_simpanan','nm_simpanan');
		$this->zetro_form->section('frmtransedit');
		$this->zetro_form->addButtons(false);
		$this->zetro_form->get_select_opt();
		$this->zetro_form->tp_ahead($this->get_akun(/*array('igrup' =>'1')*/));
		$this->zetro_form->buttons(($this->zetro_auth->cek_oto('e')!='')?true:false);
		$this->zetro_form->AddFields("<input type='hidden' id='id_trans' name='id_trans' value=''/>");
		$this->zetro_form->Modale('frm1');
		$data['modal']=$this->zetro_form->dialog($this->zetro_form->frm_std('frm1'),'Transaksi Detail');
		//load view to screen
		$this->load->view('admin/header');
		$this->load->view('accounting/transaksi_list',$data);
		$this->load->view('admin/footer');
	}
	/**
	Get data transaksi
	@param 	: fr_date to date
	@param	: find by...
	*/
	function getlisttransaksi()
	{
		$data=array();$n=0; $content="";
		$fr_date=empty($_POST['from_tgl'])?date('Ymd'):tglToSql($_POST['from_tgl']);
		$to_date=empty($_POST['upto_tgl'])?$fr_date:tglToSql($_POST['upto_tgl']);
		$where="tgl_trans between '".$fr_date."' and '". $to_date."'";
		$ordby=empty($_POST['ordby'])?'id_trans':$_POST['ordby'];
		$this->main_model->table_name('transaksi_member_v');
		empty($_POST['cari'])?
		$this->main_model->where($where):
		$this->main_model->where_like('nm_member',$_POST['cari'],'both');
		empty($_POST['tp'])?'':
		$this->main_model->where_in('jenis_trans',array('Simpanan','Pengambilan','Pinjaman'));
		$this->main_model->orderby($ordby);
		$this->main_model->query();
		$this->main_model->set_methods('select_ordered');
		$lst_usr=$this->main_model->set_data();
		$this->zetro_auth->menus('list_transaksi');
		foreach($lst_usr as $r)
		{
			$n++;
			$icon=($this->zetro_auth->cek_oto('e'))?
					$this->zetro_form->z_icon($r->id_trans,array('cus-folder','cus-cross'),array('edit','delete')):
					$this->zetro_form->z_icon($r->id_trans,array('cus-zoom','cus-'),array('info',''));
			$content.=tr().td($n,'',false,'','center').
					  td(tglfromSql($r->tgl_trans),'',false,'','center').
					  td($r->doc_no,'',false,'','center').
					  td($r->ket_trans).
					  td(number_format((int)$r->kredit,2),'',false,'','right').
					  td(number_format((int)$r->debet,2),'',false,'','right').
					  td($icon,'',false,'','center')._tr();
		}
			$content.=$this->zetro_form->DataNotFound($lst_usr,7);
			echo $content;
	}
	/**
	Get Detail transakasi
	@param	:id_trans
	*/
	function getdetailtrans()
	{
		$lst_usr=array();
		$id=$_POST['id_trans'];
		$this->main_model->table_name('transaksi_member_v');
		$this->main_model->query();
		$this->main_model->where(array('id_trans'=>$id));
		$this->main_model->set_methods('select');
		$lst_usr=$this->main_model->set_data();
		echo ($lst_usr)?json_encode($lst_usr[0]):'';
	}
	function hapustransaksi()
	{
		$id=$_POST['id_trans'];
		$link_id=empty($_POST['link_id'])?$id:$_POST['link_id'];
		$this->main_model->table_name('transaksi_member');
		$this->main_model->set_methods('delete');
		$this->main_model->where(array('id_trans'=>$id));
		$this->main_model->where_or(array('id_trans'=>$link_id));
		$this->main_model->where_or(array('link_id'=>$id));
		echo success($this->main_model->set_data());	
	}
	
	/**
		collect data akun to array for typeahead purpose
	*/
	function get_akun($where='')
	{
		$data=array();$akuns="";
		$this->main_model->where($where);
		$this->main_model->table_name('v_akun_display');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$data=$this->main_model->set_data();
		//$akuns='[';
		foreach($data as $r)
		{
			$akuns.="&quot;".$r->kodeakun."&quot;,";
		}
		//$akuns.=']';
		return substr($akuns,0,-1);
	}
	function kontraakun()
	{
		/**
			data akun
		*/
		$data=array();$akuns="";$nama="";
		$kd=$_POST['st'];
		//$this->main_model->where(array('app_stat'=>"R"));
		$this->main_model->table_name('v_akun_display');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$data=$this->main_model->set_data();
		foreach($data as $r)
		{
			$akuns[]=$r->kodeakun;
		}
		echo json_encode($akuns);
	}	/**
		collect data akun to array for typeahead purpose
		@output		: JSON
	*/
	function getakunreg()
	{
		$data=array();$akuns="";$nama="";
		$kd=$_POST['st'];
		$this->main_model->where(array('app_stat'=>"R"));
		$this->main_model->table_name('member');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$data=$this->main_model->set_data();
		foreach($data as $r)
		{
			$akuns[]=$r->id_reg;
			$nama[]=$r->nm_member;
		}
		echo ($kd=="R")?json_encode($akuns):json_encode($nama);
	}
	function getakunmember()
	{
		$data=array();$akuns="";$nama="";
		$kd=$_POST['st'];
		$this->main_model->where(array('app_stat'=>"Y"));
		$this->main_model->table_name('member');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$data=$this->main_model->set_data();
		foreach($data as $r)
		{
			$akuns[]=$r->id_reg;
			$nama[]=$r->nm_member;
		}
		echo ($kd=="R")?json_encode($akuns):json_encode($nama);
	}
	/**
	*	Transaksi penbgeluaran dan pendapatan kas
	*	operasional seharai -hari
	*/
	
	function cashadvance()
	{
		$data['tab_header']=$this->zetro_form->generate_tab_header('Transaksi Kas');
		//generate form and new lokasi
		$this->zetro_form->frm_file('asset/frm/zetro_simpanan.frm');
		$this->zetro_auth->menus('cash_advance');
			$this->zetro_form->addButtons();
			$this->zetro_form->tp_ahead($this->get_akun());		
			$this->zetro_form->section('frmcashadvance');
			$this->zetro_form->buttons(($this->zetro_auth->cek_oto('c')!='')?false:false,'Simpan');
			$this->zetro_form->frm_std('frm3','80%',true,'frm');		
			$data['modal']=$this->zetro_form->dialog($this->zetro_form->frm_std('frm3','80%'),'Add Transaksi','',
						($this->zetro_auth->cek_oto('c')!='')?true:false);

		$this->zetro_form->section('frmcashad');
		$data['frm3']=$this->zetro_form->frmInline('frm1',true,false,true);
		$data['lok']=$this->zetro_form->generate_dropdown_content('akun_unitbussiness','id_unit','nm_unit');
		//form tarik total
			$content='';
			$this->zetro_form->section('listcashadvance');
			$this->zetro_form->action_col(true);
			$this->zetro_form->tabel_content($content);
			$data['list']=$this->zetro_form->list_std();
			
		$this->load->view('admin/header');
		$this->load->view('accounting/cashadvance',$data);
		$this->load->view('admin/footer');
	}
	
	function setcashadvance()
	{
		$data=array();
		$stat=empty($_POST['stat_trans'])?'0':$_POST['stat_trans'];
		$tgl=explode("/",empty($_POST['tgl_trans'])?date('d/m/Y'):($_POST['tgl_trans']));
		$data['id_unit']	=empty($_POST['id_unit'])?'1':$_POST['id_unit'];
		$data['id_trans']   =empty($_POST['id_trans'])?'0':$_POST['id_trans'];
		$data['tgl_trans']  =empty($_POST['tgl_trans'])?date('Ymd'):tgltoSql($_POST['tgl_trans']);
		$data['jenis_trans']=empty($_POST['tp_setoran'])?'Cash':$_POST['tp_setoran'];
		$data['bln']		=$tgl[1];
		$data['tahun']	  =$tgl[2];
		$data['id_member']  =empty($_POST['no_member'])?'':$_POST['no_member'];
		$data['id_simpanan']=empty($_POST['tp_simpanan'])?'':$_POST['tp_simpanan'];
		$data['kredit']	 =($stat==1)?str_replace(",","",$_POST['jml_trans']):'0';
		$data['debet']      =($stat==0)?str_replace(",","",$_POST['jml_trans']):'0';
		$data['id_akun']    =empty($_POST['kd_akun'])?'':$_POST['kd_akun'];
		$data['id_akun_kontra']=empty($_POST['kd_akun2'])?'':$_POST['kd_akun2'];
		$data['ket_trans']  =empty($_POST['ket_trans'])?'':$_POST['ket_trans'];
		$data['reff_doc']   =empty($_POST['no_reff'])?'':$_POST['no_reff'];
		$data['status_trans']=($stat==0)?'1':'0';
		$data['created_by'] =$this->session->userdata("userid");
		$this->main_model->prepared_data('transaksi_member',$data);
		echo success($this->main_model->set_data());
		
	}
	function setcashadvance_kontra()
	{
		$data=array();
		$stat=empty($_POST['stat_trans'])?'0':$_POST['stat_trans'];
		$tgl=explode("/",empty($_POST['tgl_trans'])?date('d/m/Y'):($_POST['tgl_trans']));
		$data['id_unit']	=empty($_POST['id_unit'])?'1':$_POST['id_unit'];
		$data['id_trans']   =empty($_POST['id_trans'])?'0':$_POST['id_trans'];
		$data['tgl_trans']  =empty($_POST['tgl_trans'])?date('Ymd'):tgltoSql($_POST['tgl_trans']);
		$data['jenis_trans']=empty($_POST['tp_setoran'])?'Cash':$_POST['tp_setoran'];
		$data['bln']		=$tgl[1];
		$data['tahun']	  =$tgl[2];
		$data['id_member']  =empty($_POST['no_member'])?'':$_POST['no_member'];
		$data['id_simpanan']=empty($_POST['tp_simpanan'])?'':$_POST['tp_simpanan'];
		$data['kredit']	 =($stat==0)?str_replace(",","",$_POST['jml_trans']):'0';
		$data['debet']      =($stat==1)?str_replace(",","",$_POST['jml_trans']):'0';
		$data['id_akun']    =empty($_POST['kd_akun2'])?'':$_POST['kd_akun2'];
		$data['id_akun_kontra']=empty($_POST['kd_akun'])?'':$_POST['kd_akun'];
		$data['ket_trans']  =empty($_POST['ket_trans'])?'':$_POST['ket_trans'];
		$data['reff_doc']   =empty($_POST['no_reff'])?'':$_POST['no_reff'];
		$data['created_by'] =$this->session->userdata("userid");
		$data['link_id']	 =$this->main_model->rdb('transaksi_member','id_trans',"order by id_trans desc limit 1");
		$data['status_trans']=($stat==0)?'1':'0';
		$this->main_model->prepared_data('transaksi_member',$data);
		echo success($this->main_model->set_data());
		
	}	
	function getlistcashadvance()
	{
		$data=array();$n=0; $content="";
		$fr_date=empty($_POST['from_tgl'])?date('Ymd'):tglToSql($_POST['from_tgl']);
		$to_date=empty($_POST['upto_tgl'])?$fr_date:tglToSql($_POST['upto_tgl']);
		$where="tgl_trans between '".$fr_date."' and '". $to_date."'";
		$ordby=empty($_POST['ordby'])?'id_trans':$_POST['ordby'];
		$this->main_model->table_name('transaksi_member_v_cash');
		if(empty($_POST['cari'])){
		$this->main_model->where($where);}else{
		$this->main_model->where_like('ket_trans',$_POST['cari'],'both');
		$this->main_model->where_or_like('kredit',$_POST['cari'],'both');
		$this->main_model->where_or_like('debet',$_POST['cari'],'both');
		}
		empty($_POST['id_united'])?'':$this->main_model->where(array('id_unit'=>$_POST['id_united']));
		$this->main_model->where(array('jenis_trans'=>'Cash'));
		$this->main_model->orderby($ordby);
		$this->main_model->query();
		$this->main_model->set_methods('select_ordered');
		$lst_usr=$this->main_model->set_data();
		$this->zetro_auth->menus('list_transaksi');
		foreach($lst_usr as $r)
		{
			$n++;
			$icon=($this->zetro_auth->cek_oto('e'))?
					$this->zetro_form->z_icon($r->id_trans.":".$r->link_id,array('cus-folder','cus-cross'),array('edit','delete')):
					$this->zetro_form->z_icon($r->id_trans,array('cus-zoom','cus-'),array('info',''));
			$content.=tr().td($n,'',false,'','center').
					  td(tglfromSql($r->tgl_trans),'',false,'','center').
					  td($r->doc_no,'',false,'','center').
					  td($r->ket_trans).
					  td(number_format((int)$r->kredit,2),'',false,'','right').
					  td(number_format((int)$r->debet,2),'',false,'','right').
					  td($r->id_akun).
					  td($icon,'',false,'','center')._tr();
		}
			$content.=$this->zetro_form->DataNotFound($lst_usr,7);
			echo $content;
		
	}
	
	function getdatailcashadvance()
	{
		$lst_usr=array();
		$id=$_POST['id_trans'];
		$this->main_model->table_name('transaksi_member_v_cash');
		$this->main_model->query();
		$this->main_model->where(array('id_trans'=>$id));
		$this->main_model->set_methods('select');
		$lst_usr=$this->main_model->set_data();
		echo ($lst_usr)?json_encode($lst_usr[0]):'';
	}
	//laporan
	
	function lapbiayaoperasional()
	{
		$data['tab_header']=$this->zetro_form->generate_tab_header('Laporan Operasional');
		//generate form and new lokasi
		$this->zetro_form->frm_file('asset/frm/zetro_simpanan.frm');
		$this->zetro_auth->menus('cash_advance_list');
		$this->zetro_form->section('frmcashad');
		$data['frm3']=$this->zetro_form->frmInline('frm1',true,true,true);
		$data['lok']=$this->zetro_form->generate_dropdown_content('akun_unitbussiness','id_unit','nm_unit');
		//form tarik total
			$content='';
			$this->zetro_form->section('listoperasional');
			$this->zetro_form->action_col(false);
			$this->zetro_form->tabel_content($content);
			$data['list']=$this->zetro_form->list_std();
		$data['modal']=$this->zetro_form->PrintPreview('Laporan Transaksi Kas');	
		$this->load->view('admin/header');
		$this->load->view('accounting/lap_operasional',$data);
		$this->load->view('admin/footer');
	}
	function gettransaksicash()
	{
		$data=array();$n=0; $content="";
		$fr_date=empty($_POST['from_tgl'])?date('Ymd'):tglToSql($_POST['from_tgl']);
		$to_date=empty($_POST['upto_tgl'])?$fr_date:tglToSql($_POST['upto_tgl']);
		$where="tgl_trans between '".$fr_date."' and '". $to_date."'";
		$ordby=empty($_POST['ordby'])?'id_trans':$_POST['ordby'];
		$this->main_model->table_name('transaksi_member_v_opr');
		if(empty($_POST['cari'])){
		$this->main_model->where($where);}else{
		$this->main_model->where_like('ket_trans',$_POST['cari'],'both');
		$this->main_model->where_or_like('kredit',$_POST['cari'],'both');
		$this->main_model->where_or_like('debet',$_POST['cari'],'both');
		}
		empty($_POST['id_united'])?'':$this->main_model->where(array('id_unit'=>$_POST['id_united']));
		$this->main_model->orderby($ordby);
		$this->main_model->query();
		$this->main_model->set_methods('select_ordered');
		$lst_usr=$this->main_model->set_data();
		$this->zetro_auth->menus('list_transaksi');
		foreach($lst_usr as $r)
		{
			$n++;
			$icon=($this->zetro_auth->cek_oto('e'))?
					$this->zetro_form->z_icon($r->id_trans.":".$r->link_id,array('cus-folder','cus-cross'),array('edit','delete')):
					$this->zetro_form->z_icon($r->id_trans,array('cus-zoom','cus-'),array('info',''));
			$content.=tr().td($n,'',false,'','center').
					  td(tglfromSql($r->tgl_trans),'',false,'','center').
					  td($r->doc_no,'',false,'','center').
					  td($r->ket_trans).
					  td(number_format(($r->status_trans==0)?(int)$r->debet:(int)$r->kredit,2),'',false,'','right').
					  td(($r->status_trans==0)?'Debet':'Kredit','',false,'','center').
					  _tr();
		}
			$content.=$this->zetro_form->DataNotFound($lst_usr,7);
			echo $content;
		
	}
	function printlapoperasional()
	{
		$data=array();
		$data['coy']	=$this->zetro_form->infoCo('Nama');
		$data['add']	=$this->zetro_form->infoCo('Address');
		$data['kota']   =$this->zetro_form->infoCo('City');
		$data['keua']   =$this->zetro_form->infoCo('AccHead');
		$fr_date=empty($_POST['from_tgl'])?date('Ymd'):tglToSql($_POST['from_tgl']);
		$to_date=empty($_POST['upto_tgl'])?$fr_date:tglToSql($_POST['upto_tgl']);
		$where="tgl_trans between '".$fr_date."' and '". $to_date."'";
		$ordby=empty($_POST['ordby'])?'id_trans':$_POST['ordby'];
		$this->main_model->table_name('transaksi_member_v_opr');
		if(empty($_POST['cari'])){
			$this->main_model->where($where);}else{
			$this->main_model->where_like('ket_trans',$_POST['cari'],'both');
			$this->main_model->where_or_like('kredit',$_POST['cari'],'both');
			$this->main_model->where_or_like('debet',$_POST['cari'],'both');
		}
		empty($_POST['id_united'])?'':$this->main_model->where(array('id_unit'=>$_POST['id_united']));
		$this->main_model->orderby($ordby);
		$this->main_model->query();
		$this->main_model->set_methods('select_ordered');
		$data['frm_date']=empty($_POST['from_tgl'])?date('d/m/Y'):($_POST['from_tgl']);
		$data['to_date']=empty($_POST['upto_tgl'])?$fr_date:($_POST['upto_tgl']);
		$data['unit_bus']=empty($_POST['id_united'])?'All':
						$this->main_model->rdb("akun_unitbussiness",'nm_unit',"where id_unit='".$_POST['id_united']."'");
		$data['lst_usr']=$this->main_model->set_data();
		//print_r($data);
		$this->load->library('zetro_dompdf');
		$html = $this->load->view('accounting/lap_transaksi_kas', $data, true);
		$this->zetro_dompdf->createPDF($html, $this->session->userdata('userid').'_lap_transaksi_kas');
		echo base_url().'application/logs/'.$this->session->userdata('userid').'_lap_transaksi_kas.pdf';
		
	}
	
//end of class accounting
}